class TasksController < ApplicationController

  load_and_authorize_resource
  
  # GET /tasks
  # GET /tasks.xml
  def index
    save_redirect_back request.request_uri

    @tasks = search(params[:search_name], params[:page])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @tasks }
    end
  end

  # GET /tasks/1
  # GET /tasks/1.xml
  def show
    @task = Task.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @task }
    end
  end

  # GET /tasks/new
  # GET /tasks/new.xml
  def new
    @task = Task.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @task }
    end
  end

  # GET /tasks/1/edit
  def edit
    @task = Task.find(params[:id])
  end

  # POST /tasks
  # POST /tasks.xml
  def create
    @task = Task.new(params[:task])
    @task.is_running = false

    respond_to do |format|
      if @task.save
        flash[:notice] = t('msg_record_successfully_created')
        format.html { redirect_to(@task) }
        format.xml  { render :xml => @task, :status => :created, :location => @task }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @task.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /tasks/1
  # PUT /tasks/1.xml
  def update
    @task = Task.find(params[:id])

    respond_to do |format|
      if @task.update_attributes(params[:task])
        flash[:notice] = t('msg_record_successfully_updated')
        format.html { redirect_to(@task) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @task.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /tasks/1
  # DELETE /tasks/1.xml
  def destroy
    @task = Task.find(params[:id])

    #control al borrar
    ok = true
    tables_violate_foreign_key = []
    if  !@task.scheduled_tasks.nil?
      tables_violate_foreign_key << t('activerecord.models.scheduled_task')
    end

    if tables_violate_foreign_key.size > 0
      ok = false
      flash[:error] = "#{t('lbl_error_delete_row_violate_foreign_key')} #{tables_violate_foreign_key.join(', ')}"
    end

    if ok
      @task.destroy
    end

    respond_to do |format|
      format.html { redirect_to(tasks_url) }
      format.xml  { head :ok }
    end
  end

  def redirect_back
      redirect_back_or_default params[:url]
  end


  #Pongo aca este metodo porque no quiero tocar el modelo que pertenece al plugin
  def search(search_name, page)
    items_per_page = count_record_per_page :tasks
    order = "name"

    conditions_clauses = []
    conditions_options = []

    if !search_name.nil? && search_name.size != 0
      conditions_clauses << "upper(name) like upper(?)"
      conditions_options << "%#{search_name}%"
    end
    conditions = [conditions_clauses.join(" AND "), conditions_options].flatten
    tasks = Task.paginate :per_page => items_per_page,:page => page, :conditions => conditions, :order => order
    return tasks
  end

end
